export function useNavigator() {
	const goto = (e: any) => {
		let url = e.currentTarget.dataset.url
		const opentype = e.currentTarget.dataset.opentype || 'navigate'

		if (!url) return

		if (!url.startsWith('/')) {
			url = `/${url}`
		}

		if (['navigate', 'navigateTo'].includes(opentype)) {
			uni.navigateTo({
				url,
			})
		}
	}

	const goback = (fn?: string) => {
		if (fn) {
			const pages = getCurrentPages()
			if (pages.length > 1) {
				const page = pages[pages.length - 2]

				page.$vm[fn]?.()
				page.$vm.$.exposed[fn]?.()
			}
		}
		uni.navigateBack()
	}

	return {
		goto,
		goback,
	}
}
